package com.movitech.mobile.cz.base.purchase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.aop.ServiceThrowable;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.modules.config.PurchaseCommonEnum;
import com.movitech.mobile.cz.base.purchase.entity.WFlowFormBidCandidateUnit;
import com.movitech.mobile.cz.base.purchase.entity.ext.ExtWFlowFormBidCandidateUnit;
import com.movitech.mobile.cz.base.purchase.mapper.WFlowFormBidCandidateUnitMapper;
import com.movitech.mobile.cz.base.purchase.mapper.ext.ExtWFlowFormBidCandidateUnitMapper;
import com.movitech.mobile.cz.base.purchase.service.WFlowFormBidCandidateUnitService;
import com.movitech.mobile.cz.base.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 流程表单-投标候选单位 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-03-15
 */
@Service
public class WFlowFormBidCandidateUnitServiceImpl extends ServiceImpl<WFlowFormBidCandidateUnitMapper, WFlowFormBidCandidateUnit> implements WFlowFormBidCandidateUnitService {
    @Autowired
    private ExtWFlowFormBidCandidateUnitMapper extWFlowFormBidCandidateUnitMapper;
    @Override
    public List<ExtWFlowFormBidCandidateUnit> getExtFormList(WFlowFormBidCandidateUnit wFlowFormBidCandidateUnit) {
        return extWFlowFormBidCandidateUnitMapper.selectExtFormList(wFlowFormBidCandidateUnit);
    }

    @Override
    public List<WFlowFormBidCandidateUnit> getByFormId(String formId) {
        return selectList(new EntityWrapper<WFlowFormBidCandidateUnit>().eq("flow_form_id",formId).eq("del_flag",0));
    }

    @Override
    public WFlowFormBidCandidateUnit add(WFlowFormBidCandidateUnit wFlowFormBidCandidateUnit) {
        wFlowFormBidCandidateUnit.setBaseFeilds(UserUtils.getCurrentUserId(),null,0,true);
        boolean result=insert(wFlowFormBidCandidateUnit);
        if(!result){
            throw new RuntimeException("插入失败",new ServiceThrowable());
        }
        return wFlowFormBidCandidateUnit;
    }

    @Override
    public List<WFlowFormBidCandidateUnit> findByFormIdAndType(String formId, Integer type) {
        EntityWrapper entityWrapper = new EntityWrapper();
        WFlowFormBidCandidateUnit wFlowFormBidCandidateUnit = new WFlowFormBidCandidateUnit();
        wFlowFormBidCandidateUnit.setFlowFormId(formId);
        wFlowFormBidCandidateUnit.setCandidateType(type);
        wFlowFormBidCandidateUnit.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        entityWrapper.setEntity(wFlowFormBidCandidateUnit);
       return selectList(entityWrapper);
    }

    /**
     * 获取中标候选人
     *
     * @param formId
     * @return
     */
    @Override
    public List<ExtWFlowFormBidCandidateUnit> getAwardBidByFormId(String formId) {
        WFlowFormBidCandidateUnit wFlowFormBidCandidateUnit = new WFlowFormBidCandidateUnit();
        wFlowFormBidCandidateUnit.setFlowFormId(formId);
        wFlowFormBidCandidateUnit.setDelFlag(CommonConstants.DEL_FLAG_NORMAL);
        wFlowFormBidCandidateUnit.setCandidateType(PurchaseCommonEnum.CANDIDATE_TYPE_IN.getCode());
        return  getExtFormList(wFlowFormBidCandidateUnit);
    }

	@Override
	public WFlowFormBidCandidateUnit getBidCandidateUnitByFormIdAndSupplierId(String flowFormId, String suppliersId) {
		WFlowFormBidCandidateUnit paramEntity = new WFlowFormBidCandidateUnit();
		paramEntity.setFlowFormId(flowFormId);
		paramEntity.setSuppliersId(suppliersId);
		paramEntity.setDelFlag(CommonConstants.NO_INTEGER);
		return baseMapper.selectOne(paramEntity);
	}
}
